package com.bsj.travel.device.web.controller;

import com.alibaba.fastjson2.JSON;
import com.bsj.travel.cached.redis.RedisCached;
import com.bsj.travel.constant.NumericalConstants;
import com.bsj.travel.constants.RedisConstant;
import com.bsj.travel.def.common.DO.ProductDO;
import com.bsj.travel.def.common.DO.ProductTopicDO;
import com.bsj.travel.def.common.DTO.web.product.ProductDTO;
import com.bsj.travel.def.common.DTO.web.product.ProductSearchDTO;
import com.bsj.travel.def.common.VO.ProductVO;
import com.bsj.travel.def.global.JsonResult;
import com.bsj.travel.device.service.ProductService;
import com.bsj.travel.device.service.ProductTopicService;
import com.bsj.travel.device.service.TableTDengineService;
import com.bsj.travel.mapper.mysql.ProductMapper;
import com.bsj.travel.security.annotation.InnerAuth;
import com.bsj.travel.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yinhao
 * @version 1.0
 * @description 产品类管理类
 * @date 2023/11/14
 */
@RestController
@RequestMapping(value = "/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTopicService productTopicService;

    @Autowired
    private TableTDengineService tableTDengineService;

    @Autowired
    private RedisCached redisCached;

    @Resource
    private ProductMapper productMapper;

    /**
     * 添加产品
     *
     * @param productDTO
     * @return
     */
    @PostMapping(value = "/add")
    @RequiresPermissions(value = "product:operation:add")
    public JsonResult add(@Validated ProductDTO productDTO) {
        return tableTDengineService.createSuperTable(productService.insertProduct(productDTO));
    }

    /**
     * 删除产品
     *
     * @param productId
     * @return int
     */
    @PostMapping(value = "/delete/{productId}")
    @RequiresPermissions(value = "product:operation:delete")
    public JsonResult delete(@PathVariable Long productId) throws Exception {
        return JsonResult.success(productService.deleteProductById(productId));
    }

    /**
     * 修改产品
     *
     * @param productDTO
     * @return com.bsj.travel.def.global.JsonResult
     * @author ljx
     * @time 2024/1/17 15:21
     */
    @PostMapping("/update")
    @RequiresPermissions(value = "product:operation:update")
    public JsonResult update(@Validated ProductDTO productDTO) {
        return productService.updateProduct(productDTO);
    }

    /**
     * 根据产品id获取产品信息
     *
     * @param productId
     * @return com.bsj.travel.def.global.JsonResult<com.bsj.travel.def.common.VO.ProductVO>
     * @author ljx
     * @time 2024/1/17 15:19
     */
    @GetMapping("/getById/{productId}")
    @RequiresPermissions(value = "product:info:getById")
    public JsonResult<ProductVO> getById(@PathVariable("productId") Long productId) {
        return JsonResult.success(productService.getProductInfoById(productId));
    }

    /**
     * 产品分页
     *
     * @return
     */
    @GetMapping(value = "/page")
    @RequiresPermissions(value = "product:info:page")
    public JsonResult<List<ProductVO>> page(@Validated ProductSearchDTO productSearchDTO) {
        return productService.pageProduct(productSearchDTO);
    }

    /**
     * 获取对应产品的所有主题数据
     *
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/topic/getAllList/{productKey}")
    @InnerAuth
    public JsonResult<List<ProductTopicDO>> getAllList(@PathVariable("productKey") String productKey) throws Exception {
        return JsonResult.success(productTopicService.getAllList(productKey));
    }

    /**
     * 获取产品列表
     *
     * @param type 1查所有 2查当前登录用户所有
     * @return
     */
    @GetMapping(value = "/list/{type}")
    @InnerAuth
    public JsonResult<List<ProductDO>> list(@PathVariable("type") Integer type) {
        return JsonResult.success(productService.selectProductList(type));
    }

    /**
     * 获取产品列表
     *
     * @param
     * @return com.bsj.travel.def.global.JsonResult<java.util.List < com.bsj.travel.def.common.DO.ProductDO>>
     * @author ljx
     * @time 2024/2/21 14:18
     */
    @GetMapping(value = "/list")
    public JsonResult<List<ProductDO>> list() {
        return JsonResult.success(productService.selectProductList(NumericalConstants.TWO));
    }

    @GetMapping("/test")
    public void test() {
        List<ProductDO> productDOS = productMapper.selectList(null);
        for (ProductDO productDO : productDOS) {
            System.out.println(JSON.toJSONString(productDO));
            redisCached.hset(RedisConstant.PRODUCT_INFO_KEY, productDO.getProductKey(), JSON.toJSONString(productDO));
        }
    }

}
